package com.example.web.controller;

import com.example.web.entity.Dictionary;
import com.example.web.entity.Result;
import com.example.web.service.DictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 字典管理控制器
 */
@RestController
@RequestMapping("/api/dictionary")
public class DictionaryController {
    
    @Autowired
    private DictionaryService dictionaryService;
    
    /**
     * 查询字典列表（分页）
     */
    @GetMapping
    public Result<Map<String, Object>> getDictionaryList(
            @RequestParam(value = "dictType", required = false) String dictType,
            @RequestParam(value = "dictName", required = false) String dictName,
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "10") int size) {
        return dictionaryService.getDictionaryList(dictType, dictName, page, size);
    }
    
    /**
     * 根据ID查询字典
     */
    @GetMapping("/{id}")
    public Result<Dictionary> getDictionaryById(@PathVariable Long id) {
        return dictionaryService.getDictionaryById(id);
    }
    
    /**
     * 根据字典类型查询字典列表
     */
    @GetMapping("/type/{dictType}")
    public Result<List<Dictionary>> getDictionaryByType(@PathVariable String dictType) {
        return dictionaryService.getDictionaryByType(dictType);
    }
    
    /**
     * 创建字典
     */
    @PostMapping
    public Result<String> createDictionary(@RequestBody Dictionary dictionary) {
        return dictionaryService.createDictionary(dictionary);
    }
    
    /**
     * 更新字典
     */
    @PutMapping("/{id}")
    public Result<String> updateDictionary(@PathVariable Long id, @RequestBody Dictionary dictionary) {
        dictionary.setId(id);
        return dictionaryService.updateDictionary(dictionary);
    }
    
    /**
     * 删除字典
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteDictionary(@PathVariable Long id) {
        return dictionaryService.deleteDictionary(id);
    }
    
    /**
     * 导出字典数据
     */
    @GetMapping("/export")
    public void exportDictionary(HttpServletResponse response) {
        dictionaryService.exportDictionary(response);
    }
    
    /**
     * 导入字典数据
     */
    @PostMapping("/import")
    public Result<String> importDictionary(@RequestParam("file") MultipartFile file) {
        return dictionaryService.importDictionary(file);
    }
}